package com.qgs.secsgem.mq.producer.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qgs.secsgem.common.utils.CommonFunction;
import com.qgs.secsgem.common.utils.R;
import com.qgs.secsgem.mq.configuration.ConfigOfPID10_1;
import com.qgs.secsgem.mq.entity.CallbackMessageEntity;
import com.qgs.secsgem.mq.entity.MQMessage;
import com.qgs.secsgem.mq.entity.common.Header;
import com.qgs.secsgem.mq.entity.common.Returns;
import com.qgs.secsgem.mq.entity.specificBody.QueryEQStatusBody;
import com.qgs.secsgem.mq.producer.component.RabbitSender;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2020/9/30 11:03
 */
@RestController
public class TestController {
    @Autowired
    RabbitSender rabbitSender;

    @PostMapping("/eapRequest")
    /**
     *  desc : 回复EAP的request 的回调接口（向rabbitmq中发送消息（direct模式））
     *  MES => DC(数据采集中间件) => MQ => EAP
     */
    public R eapRequest(){
        try {
            //properties 这里的参数是写在MQ消息 header里面的，如果EAP端 需要某些参数 可以写在这里，eap去取更方便一些
            for(int i =0;i<20;i++){
                Map<String,Object> properties = new HashMap<>();
                CallbackMessageEntity callbackMessageEntity = new CallbackMessageEntity();
                properties.put("equipmentName","PID001");
                callbackMessageEntity.setExchangeName("00B_Exchange");
                callbackMessageEntity.setRoutingKey("00B_EAP_Request_Queue_RoutingKey");

                MQMessage mesResponse = new MQMessage();

                Header header = new Header("Request","Execute","QUERYEQPStatus","12");
                QueryEQStatusBody queryEQStatusBody = new QueryEQStatusBody();
                queryEQStatusBody.setVidType("u4");
                List<String> vids = new ArrayList<>();
                vids.add("10000");
                vids.add("10001");
                vids.add("10002");
                queryEQStatusBody.setVidList(vids);
                mesResponse.setBody(JSONObject.toJSONString(queryEQStatusBody));
                mesResponse.setHeader(header);
                callbackMessageEntity.setMqMessage(mesResponse);
                properties.put("transitionId", "12312");
                rabbitSender.reply(callbackMessageEntity.getMqMessage(),properties, callbackMessageEntity.getExchangeName(), callbackMessageEntity.getRoutingKey());

            }

            return R.ok("回复成功");
        }catch (Exception e){
            return R.error().put("result",e.getMessage());
        }
    }

    public void reply(MQMessage request) throws Exception {
        MQMessage response = new MQMessage();

        Header header = request.getHeader();
        header.setFrom("mes");
        header.setTo("eap");
        header.setMessageType("Response");
        header.setSendTimestamp(CommonFunction.getNowDate(1));
        Returns aReturn = new Returns("0","0");

        response.setHeader(header);
        response.setReturns(aReturn);

        rabbitSender.sendDirect(response, ConfigOfPID10_1.EXCHANGE_NAME_PID10_1,ConfigOfPID10_1.EAP_RESPONSE_QUEUE_ROUTINGKEY_PID10_1);
        System.out.println("回复的消息是: "+response.toString());
    }

    @PostMapping("/testS1F3")
    public void testS1F3() throws Exception {
        MQMessage mesResponse = new MQMessage();

        Header header = new Header("Request","Execute","QUERYEQStatus","12");
        QueryEQStatusBody queryEQStatusBody = new QueryEQStatusBody();
        queryEQStatusBody.setVidType("U4");
        List<String> vids = new ArrayList<>();
        vids.add("10000");
        vids.add("10001");
        vids.add("10002");
        queryEQStatusBody.setVidList(vids);
        mesResponse.setBody(JSONObject.toJSONString(queryEQStatusBody));
        mesResponse.setHeader(header);
        rabbitSender.sendDirect(mesResponse, ConfigOfPID10_1.EXCHANGE_NAME_PID10_1,ConfigOfPID10_1.MES_REQUEST_QUEUE_ROUTINGKEY_PID10_1);
    }

    public static void main(String[] args) throws IOException {




        Map<String,Object> properties = new HashMap<>();
        properties.put("attr1",String.valueOf(1));
        properties.put("attr2","00B");
        //序列化
        MQMessage mqMessage = new MQMessage();

        Header header = new Header("Request","Execute","QUERYEQPStatus","12");
        header.setTransactionId("abc123456");
        QueryEQStatusBody queryEQStatusBody = new QueryEQStatusBody();
        queryEQStatusBody.setVidType("u4");
        List<String> vids = new ArrayList<>();
        vids.add("10000");
        vids.add("10001");
        vids.add("10002");
        queryEQStatusBody.setVidList(vids);
        mqMessage.setBody(JSONObject.toJSONString(queryEQStatusBody));
        mqMessage.setHeader(header);
        System.out.println(mqMessage);
        //序列化
        //1.jackson
        ObjectMapper objectMapper = new ObjectMapper();
        //byte[] byteJackson = objectMapper.writeValueAsBytes(mqMessage);
        //2.fastjson
        byte[] bytes = JSONObject.toJSONBytes(mqMessage);

        //反序列化
        //1.jackson
        //MQMessage mqMessageJackson = objectMapper.readValue(byteJackson, MQMessage.class);
        //2.fastjson
        MQMessage mqMessageFastjson = JSONObject.parseObject(bytes, MQMessage.class);
        System.out.println();
    }


//    public static void main(String[] args) {
//        //序列化
//        MQMessage mesResponse = new MQMessage();
//
//        Header header = new Header("Request","Execute","QUERYEQStatus","12");
//        QueryEQStatusBody queryEQStatusBody = new QueryEQStatusBody();
//        queryEQStatusBody.setVidType("U4");
//        List<String> vids = new ArrayList<>();
//        vids.add("10000");
//        vids.add("10001");
//        vids.add("10002");
//        queryEQStatusBody.setVidList(vids);
//        mesResponse.setBody(JSONObject.toJSONString(queryEQStatusBody));
//        mesResponse.setHeader(header);
//
//        byte[] bytes = JSONObject.toJSONBytes(mesResponse);
////        String s = JSONObject.toJSONString(requestBody);
////        RequestBody parsedRequestBody = JSONObject.parseObject(bytes, RequestBody.class);
////        JSONObject jsonObject = (JSONObject)parsedRequestBody.getBody();
////        QueryEQStatusBody queryEQStatusBody1 = JSON.toJavaObject(jsonObject, QueryEQStatusBody.class);
//
//        //1.先解析成MesRequest/MesResponse ，拿到header里的messageName
////        MQMessage message = CommonFunction.parse(bytes);
////        String messageName = message.getHeader().getMessageName();
////
////        //拿到具体messageName 就可以拿到具体body
////        QueryEQStatusBody queryEQStatusBody1 = (QueryEQStatusBody)CommonFunction.parseBody(message.getBody(), QueryEQStatusBody.class);
////        System.out.println();
//    }


}
